-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add bogo sort in Golang #365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hey, sorry it's taking so long to get to this PR. My go knolwedge isn't great and we don't have too many reviewers who use the language. Thanks for the submission! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would change two things. Other than that it's good code
contents/bogo_sort/code/go/main.go
Outdated
) | ||
|
||
func shuffle(a []int) []int { | ||
rand.Seed(time.Now().UnixNano()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should rarely (I dare to say never) seed your prng more than once. I would move that line into the main.
contents/bogo_sort/code/go/main.go
Outdated
|
||
func bogo_sort(a *[]int) { | ||
for !is_sorted(*a) { | ||
*a = shuffle(*a) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you already work with pointers why not make the shuffe inplace. So basically change *a = shuffle(*a)
to shuffle(a)
.
I would say either remove all pointers and let every function return it's value or change line 15 to (*a)[i], (*a)[j] = (*a)[j], (*a)[i]
and take in a *[]int
as a parameter in shuffle.
contents/bogo_sort/code/go/main.go
Outdated
return true | ||
} | ||
|
||
func bogo_sort(a *[]int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh and as a general style rule golang uses camelcase and not underlines. Meaning the function should be called bogoSort
and not bogo_sort
Hey checking up on this PR. A review was done, but @christopherm99 has not yet updated the contents. Any further work on that? |
Sorry, this must have slipped my mind. I have submitted a commit which should resolve the requested changes. |
contents/bogo_sort/code/go/main.go
Outdated
func shuffle(a *[]int) { | ||
for i := len(*a) - 1; i > 0; i-- { | ||
j := rand.Intn(i + 1) | ||
(*a)[i], (*a)[j] = (*a)[j], (*a)[i] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line still has a tab in it.
This was compiled the
go1.10.3
compiler on linux, and raises no errors.